package com.pms.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.pms.entity.Goal;
import com.pms.entity.GoalRate;
import com.pms.util.DBHelp;
import com.pms.util.RowMapper;

public class GoalRateDao {

	DBHelp<GoalRate> db = new DBHelp<GoalRate>();

	public boolean save(GoalRate goalRate) {
		String sql = "INSERT INTO t_goalrate(goalrateid,goalid,goalrate) VALUES(?,?,?)";
		return db.executeSQL(sql, goalRate.getGoalRateId(),goalRate.getGoalId(),goalRate.getGoalRate());
		
	}

	public GoalRate findById(String goalId) {
		StringBuilder sql=new StringBuilder();
		sql.append("SELECT goalrateid,goalrate,tgr.goalid,tg.goalname,tg.`desc`,tg.createtime FROM t_goalrate AS tgr ");
		sql.append("LEFT JOIN t_goal AS tg ON tgr.goalid = tg.goalid ");
		sql.append("WHERE tgr.goalid = ? ");
		return db.executeQueryForObject(sql.toString(), new GoalRateRowMapper(), goalId);
	}

	
	
	
	
	
	
	
	
	
	
	private class GoalRateRowMapper implements RowMapper<GoalRate>{

		@Override
		public GoalRate mapRow(ResultSet rs) throws SQLException {
			GoalRate goalRate = new GoalRate();
			goalRate.setGoalRateId(rs.getString("goalRateId"));
			goalRate.setGoalRate(rs.getFloat("goalRate"));
			
			Goal goal = new Goal();
			goal.setCreateTime(rs.getString("createTime"));
			goal.setDesc(rs.getString("desc"));
			goal.setGoalId(rs.getString("goalId"));
			goal.setGoalName(rs.getString("goalName"));
			
			goalRate.setGoal(goal);
			goalRate.setGoalId(rs.getString("goalId"));
			
			return goalRate;
		}
		
	}
}
